<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of newsletter
 *
 * @author Gidaff_01
 */
class Newsletter extends Admin_Controller{
    //put your code here
    function Newsletter() {
        parent::Admin_Controller();
        $this->load->model('Newsletter_m');
        $this->load->library('pagination');
        $this->load->library('session');
        $this->load->helper('date');
        $this->load->library('email');

        $this->_data['erreur'] = "";
    }

    function index(){
        $this->liste_news();
    }

    function liste_news(){
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/newsletter/liste_news/' . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Newsletter_m->count_all_news();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['newsletter'] = $this->Newsletter_m->get_all_news($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Newsletter_m->count_all_news();
        $this->_data['page_title'] = "Liste de newsletter";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('newsletter/liste_news');
    }

    function update_newsletter($id){
        is_admin();
        $newsletter = $this->Newsletter_m->get_newsletter_detail($id);
        $this->_data['newsletter_detail'] = $newsletter;

        $this->_data['erreur'] = "";
        $config = array(
            array(
                'field' => 'titre',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'contenu',
                'Label' => 'Contenu',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);

        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';

        $this->email->initialize($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'titre' => $this->input->post('titre'),
                    'contenu' => $this->input->post('contenu'),
                    'dat' => time()
                );
                $this->Newsletter_m->update_newsletter($data, $id);

                $user_data = $this->Newsletter_m->get_all_email();
                foreach ($user_data as $user) {
                    $From = 'administrator@lehu.com';
                    $this->email->from($From, 'Zoo-market');
                    $this->email->to($user['email']);
                    $this->email->subject('Newsletter de Lehu');
                    $message = 'Hello ' . $user['display_name'].".<p> Il s'agit d'une des nouvelles de Lehu</p>";
                    $message .= $this->input->post('contenu');
                    $this->email->message($message);
//                    echo $user['email'];
//                    echo $user['display_name'];
//                    echo $message;die;
                    $this->email->send();
                }
                redirect('newsletter/liste_news');
            } else {
                $this->display_view('newsletter/update_newsletter');
            }
        } else {
                $this->display_view('newsletter/update_newsletter');
        }
    }

    function delete_newsletter($id,$per_page,$off_set){
        $this->Newsletter_m->delete_newsletter($id);
        redirect('newsletter/liste_news' . '/' . $per_page . '/' . $off_set);
    }

    function ajouter_newsletter(){
        is_admin();
        
        $this->_data['erreur'] = "";
        $config = array(
            array(
                'field' => 'titre',
                'label' => 'Titre',
                'rules' => 'required'
            ),
            array(
                'field' => 'contenu',
                'Label' => 'Contenu',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);

        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';

        $this->email->initialize($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'titre' => $this->input->post('titre'),
                    'contenu' => $this->input->post('contenu'),
                    'dat' => time()
                );
                $this->Newsletter_m->ajouter_newsletter($data);

                $user_data = $this->Newsletter_m->get_all_email();
                foreach ($user_data as $user) {
                    $From = 'administrator@lehu.com';
                    $this->email->from($From, 'Zoo-market');
                    $this->email->to($user['email']);
                    $this->email->subject('Newsletter de Lehu');
                    $message = 'Hello ' . $user['display_name'].".<p> Il s'agit d'une des nouvelles de Lehu</p>";
                    $message .= $this->input->post('contenu');
                    $this->email->message($message);
//                    echo $user['email'];
//                    echo $user['display_name'];
//                    echo $message;die;
                    $this->email->send();
                }
                redirect('newsletter/liste_news');
            } else {
                $this->display_view('newsletter/ajouter_newsletter');
            }
        } else {
                $this->display_view('newsletter/ajouter_newsletter');
        }
    }

}
?>
